﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Sina.Altman.Weibo.SDK.Objects;
using Microsoft.Phone.Shell;
using Sina.Altman.Weibo.Client.Commons;
using Sina.Altman.Weibo.Languages;
using Sina.Altman.Weibo.Common;
using Sina.Altman.Weibo.SDK;

namespace Sina.Altman.Weibo.Client.View
{
    public partial class HomePage : PhoneApplicationPage
    {
        public HomePage()
        {
            InitializeComponent();
            
            Loaded += new RoutedEventHandler(HomePage_Loaded);
        }

        void HomePage_Loaded(object sender, RoutedEventArgs e)
        {
            InitialAppBar();

            StatusesRequest.GetRequest(StateType.Home, null, ((result) =>
            {
                if (result is Status)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        VMLocator.Instance.HomeTimelineVM.HomeTimeline = result as Status;

                        this.timelineLB.ItemsSource = VMLocator.Instance.HomeTimelineVM.HomeTimeline.Statuses;
                    });
                }
                else
                {
                    //MessageBox.Show((result as Exception).Message);
                }
            }));
        }

        private ApplicationBarIconButton _sendWeiboButton;
        private ApplicationBarIconButton _refreshButton;
        
        private void InitialAppBar()
        {
            if(base.ApplicationBar==null)
            {
                base.ApplicationBar=new ApplicationBar();
                base.ApplicationBar.Buttons.Clear();
                base.ApplicationBar.MenuItems.Clear();

                this._sendWeiboButton = new ApplicationBarIconButton(new Uri(ResourceConstants.SendWeiboPic,UriKind.Relative));
                this._sendWeiboButton.Text = LangResource.SendWeibo;
                this._sendWeiboButton.Click += new EventHandler((s, e) =>
                {
                    NavigationService.Navigate(new Uri(Pages.SendWeiboPage,UriKind.Relative));
                });

                this._refreshButton = new ApplicationBarIconButton(new Uri(ResourceConstants.RefreshPic,UriKind.Relative));
                this._refreshButton.Text = LangResource.Refresh;
                this._refreshButton.Click += new EventHandler((s, e) =>
                {

                });

                this.ApplicationBar.Buttons.Add(_sendWeiboButton);
                this.ApplicationBar.Buttons.Add(_refreshButton);
            }
        }
    }
}